# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
通过查找数据库中P状态的支付信息，根据两个字段去判断是哪个支付渠道
1.不同的项目，url必须更改
2.不同的项目，db数据库必须更改
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import pymysql
import requests
import time


# 汇潮支付HC
hc_payApi = {"amount":"1.00","billNo":"GATEWAY0011052569","cardNo":"6214837836637755","merBillNo":"190219111930FtBO7Erh",
             "merNo":"46859","result":"成功","signInfo":"o67RbwnkhtW8UGArb02zbG7UK7f71zCRzWLqWAFlLSBe1eadAgoWxrHzPQFY5hGV4ohAwbMxW3GE9BGMvO3b97qi3kP+vscKMSk7+WCUlg7df3hW50Jx6BEFCp2fWS5b8ZTFkmwpAW0IPDrNBDgchyjuC3w96TbOQlm+nsolpmU=",
             "succeed":"00"}

# 易宝支付YEEPAY  status=S成功，F失败
yb_payApi = { "batch_No":"154892625602326", "order_Id":"W190131003001001bDdm_eelpeqaglOX53Dmk2KVG", "status":"S", "message":"", "cmd":"c","errorMsg":"" }

# 汇聚支付HJZF   status=205 成功，204失败
hj_payApi = {"platformSerialNo":"300319031572881615","fee":0.80,"hmac":"eO3thrl59McWnfOQmLGkDEUlbr6Dyv7GobK2hujocDmhzlj1pY4iL9fZoTxXLWQAwDk8NsUxGOGJdx1zPRvYXTky6zzLvuqU4viIC4cDIZ/CK/3NyS0mkbC3zSkEOKhWg6L4dMq/OLLdSyKZG07PTufBAlC/rJy5S7KD2UoaIJY=","userNo":"888106400004595","errorCode":"","errorCodeDesc":"","receiverAccountNoEnc":"6214837836637755",
             "merchantOrderNo":"190316135500057UoPjU5QllQ","paidAmount":0.01,"receiverNameEnc":"梁家龙","status":"205"}

# 宝付支付BFZF   state=1 成功，-1失败,0-转账中
bf_payApi = {"state":"1","to_acc_name":"梁家龙","to_acc_no":"6214837836637755","trans_batchid":"207290498","trans_endtime":"20190408173436","trans_fee":"0.50","trans_money":"0.01",
             "trans_no":"1232043190408173422k8XVwtAKiIIU","trans_orderid":"240273790","trans_remark":"","trans_starttime":"20190408173422"}

# 银盛支付YSZF   TRADE_FAILURE-失败  # x-www-form-urlencoded
ys_payApi = {"notify_time":"2019-03-13 18:52:41","payee_fee":0.0,"account_date":"20190313","fee":0.0,"sign":"K5nFGwGCNTq17QT0cVFeqNtvN7Y+wrG/crhI+r0UYUOLHdEAa9349fElU2Q7BayJzlZQiQhtPSVdLH69knqGsQOxBE111qHdUjchLHCkLjmXKaJvini8ZLWh0Q/yJzjGnpL7A4WzjsVzG1GMhEO9WZsgsjwkl28eY/V7hk5NGVs=",
             "notify_type":"ysepay.df.single.notify","partner_fee":0.0,"out_trade_no":"20190315HRIEXa9dzfVDjnNxgi10oIn6","total_amount":2000.00,"trade_no":"102190313557928650",
             "sign_type":"RSA","payer_fee":0.0,"trade_status_description":"测试成功","trade_status":"TRADE_SUCCESS"}

# 富友支付FYZF x-www-form-urlencoded
fy_payApi = {"orderno":"201903281734023155376566385993","merdt":"20190329","fuorderno":"601700566622","accntno":"6214837836637755","mac":"7d0f5f3b6383e71dd9f7b27181219ff7",
             "accntnm":"梁家龙","bankno":"0308","amt":2000.00,"state":"1","result":"渠道资金到账已复核,交易已发送","reason":""}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    'content-Type': 'application/json'
}

headers1 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    'content-Type': "application/x-www-form-urlencoded"
}

url = 'https://test.zyjk.msxiaodai.com/'              # https://test.coo.msxiaodai.com/,https://test.zyjk.msxiaodai.com/

hc_url = url + 'hc/payCallback_test'                  # 'http://39.108.104.213:800/hc/payCallback_test'  # 汇潮支付HC
yb_url = url + 'pay/payCallback_test'                 # 易宝支付YEEPAY
hj_url = url + 'huiju/payCallback_test'                # 汇聚支付HJZF

ys_url = url + 'yszf/payCallback_test'                 # 银盛支付YSZF
fyS_url = url + 'fyzf/successPayCallback_test'         # 富友支付 成功
fyF_url = url + 'fyzf/FailPayCallback_test'            # 富友支付 失败
bf_url = url + '/bfzf/payCallback_test'                # 宝付支付BFZF


db = pymysql.connect(
        host="10.16.20.182",    # 测试环境
        #host="10.16.20.190",     # 开发环境
        user="dev",             # 测试：dev   开发：root
        password="dev",       # 测试：dev   开发：123456
        port=3306,
        use_unicode=True,
        charset="utf8",
        database="msydloandb")           # qdzjmsloandb  ， bxloandb ,msydloandb
cursor = db.cursor()


def get_payOrder():
    sql = "select pay_channel,pay_no,batch_No from pay_order where status = 'P';"
    cursor.execute(sql)
    results = cursor.fetchall()
    print(len(results))
    return results

def hc_payCallback(pay_no):
    hc_payApi['merBillNo'] = pay_no
    hc_payApi['succeed'] = "11"           # 00-成功，11-失败
    req1 = requests.post(hc_url, json=hc_payApi, headers=headers)
    print("汇潮支付结果：",req1, req1.text)

def yb_payCallback(pay_no,batch_No):
    print(yb_url)
    yb_payApi['order_Id'] = pay_no
    yb_payApi['batch_No'] = batch_No
    yb_payApi['status'] = "S"
    req1 = requests.post(yb_url, json=yb_payApi, headers=headers)
    print("易宝支付结果：",req1, req1.text)

def hj_payCallback(pay_no):
    hj_payApi['merchantOrderNo'] = pay_no
    hj_payApi['status'] = "205"      # status=205 成功，204失败
    req1 = requests.post(hj_url, json=hj_payApi, headers=headers)
    print("汇聚支付结果：",req1, req1.text)

def bf_payCallback(pay_no):
    bf_payApi['trans_no'] = pay_no
    bf_payApi['state'] = "1"      # state=1 成功，-1失败
    req1 = requests.post(bf_url, json=bf_payApi, headers=headers)
    print("宝付支付结果：",req1, req1.text)

# x-www-form-urlencoded
def ys_payCallback(pay_no):
    ys_payApi['out_trade_no'] = pay_no
    ys_payApi['trade_status'] = "TRADE_SUCCESS"  # TRADE_FAILURE-失败,TRADE_SUCCESS-成功
    req1 = requests.post(ys_url, data=ys_payApi, headers=headers1)
    print("银盛支付结果：",req1, req1.text)

# x-www-form-urlencoded  支付成功
def fyS_payCallback(pay_no):
    fy_payApi['orderno'] = pay_no
    req1 = requests.post(fyS_url, data=fy_payApi, headers=headers1)
    print("富友支付成功结果：",req1, req1.text)

# x-www-form-urlencoded  支付失败
def fyF_payCallback(pay_no):
    fy_payApi['orderno'] = pay_no
    req1 = requests.post(fyF_url, data=fy_payApi, headers=headers1)
    print("富友支付失败结果：",req1, req1.text)


def main():
    data = get_payOrder()
    ''''''''''''''''''''
    # 直接运行
    for pay in data:
        print(pay[1],pay[2])
        if pay[0] == 'YEEPAY':
            yb_payCallback(pay[1], pay[2])
            time.sleep(0.1)
        elif pay[0] == 'HC':
            hc_payCallback(pay[1])
            time.sleep(0.1)
        elif pay[0] == 'YSZF':
            ys_payCallback(pay[1])
            time.sleep(0.1)
        elif pay[0] == 'HJZF':
            hj_payCallback(pay[1])
            time.sleep(0.1)
        elif pay[0] == 'FYZF':
            fyS_payCallback(pay[1])
            time.sleep(0.1)
        elif pay[0] == 'BFZF':
            bf_payCallback(pay[1])
            time.sleep(0.1)
    ''''''''''''''''''


if __name__ == '__main__':
    main()

